/** 
Copyright (c) 2010, Sheikh Abdul Wahid Ahmed
Details @ http://extremecodeworld.codeplex.com/license
**/

using System;
using System.Data;
using System.Text; 
using System.Configuration;
using System.Collections;
using System.Web;
using System.Web.Security;
using System.Web.UI;
using System.Web.UI.WebControls;
using System.Web.UI.WebControls.WebParts;
using System.Web.UI.HtmlControls;
using EXtremecode.PresentationLayer;

public partial class ASCXTextBox : System.Web.UI.UserControl, IASCXControl
{

    private bool isSetDisplayMethodCalled = false;
    
    protected void Page_Load(object sender, EventArgs e)
    {
        
    }

    public override string ClientID
    {
        get
        {
            return CurrentClientId; 
        }
    }
    protected override object SaveControlState()
    {
        object baseState = base.SaveControlState();
        /*object[] obj = new object[] {
            baseState
            ,caption
            };*/
        return baseState;
    }
    protected override void LoadControlState(object savedState)
    {
        base.LoadControlState(savedState);
    }
    
    protected void Page_PreRender(object sender, EventArgs e)
    {
        vldrRequired.ErrorMessage = string.Format("{0} : {1}",
            caption, ResourceProvider.GetGeneralResourceString("Message_FieldRequired"));

        if (DuplicateCheck && !IsReadOnly)
        {
            vldrDuplicate.Enabled = true;
            vldrDuplicate.ErrorMessage = string.Format("{0} : {1}",
                caption, ResourceProvider.GetGeneralResourceString("Message_Duplicate"));
            vldrDuplicate.Attributes.Add("ControlId", this.ClientID);

            textBox.Attributes.Add("onchange", "this.CurrentCtrl.ValidateDuplicate()");
        }
        
        //Regex validator.
        if (string.IsNullOrEmpty(vldrRegx.ErrorMessage))
        {
            vldrRegx.ErrorMessage = string.Format("{0} : {1}",
                caption, ResourceProvider.GetGeneralResourceString("Message_InvalidValue"));
        }
        if (string.IsNullOrEmpty(vldrRegx.ValidationExpression))
        {
            vldrRegx.ValidationExpression = ResourceProvider.GetExpression("Expression_NormalText");
        }

        //we want astric to be shown 
        if (textBox.TextMode == TextBoxMode.Password)
        {
            textBox.Attributes.Add("value", textBox.Text); 
        }

        //Script block
        string currentClientId=this.CurrentClientId;
        string originalClientId=this.OriginalClientId;
        string inputClientId=this.InputClientId;
        string displayClientId=this.DisplayClientId;

        StringBuilder sbScriptBlock = new StringBuilder();

        sbScriptBlock.AppendLine("<script>");
        sbScriptBlock.AppendLine(
            string.Format("var {0} = document.getElementById('{0}');",currentClientId));
        
        sbScriptBlock.AppendLine(
            string.Format("{0}.OriginalCtrl = document.getElementById('{1}');",
                currentClientId,originalClientId));

        if (!IsReadOnly)
        {
            sbScriptBlock.AppendLine(
                string.Format("var {0} = document.getElementById('{0}');", inputClientId));

            sbScriptBlock.AppendLine(
               string.Format("{0}.CurrentCtrl = document.getElementById('{1}');",
                   inputClientId, currentClientId));
        }

        
        sbScriptBlock.AppendLine(
            string.Format("{0}.InputCtrl = document.getElementById('{1}');",
            currentClientId,inputClientId));
        
        sbScriptBlock.AppendLine(
            string.Format("{0}.DisplayCtrl = document.getElementById('{1}');",
            currentClientId,displayClientId));
        
        sbScriptBlock.AppendLine(
            string.Format("{0}.FormattingObject = new EmptyClass();",currentClientId));

        sbScriptBlock.AppendLine(
            string.Format("{0}.SetValue = SetTextValue;",currentClientId));

        sbScriptBlock.AppendLine(
            string.Format("{0}.GetValue = GetTextValue;",currentClientId));

        sbScriptBlock.AppendLine(
            string.Format("{0}.ResetValue = resetValue;", currentClientId));

        
        
        //these are required for webservice calling.
        if (DuplicateCheck && !IsReadOnly)
        {
            sbScriptBlock.AppendLine(
                string.Format("{0}.EntityNameDuplicateCheck = '{1}';", currentClientId, entityNameDuplicateCheck));

            sbScriptBlock.AppendLine(
                string.Format("{0}.FieldNameDuplicateCheck = '{1}';",
                currentClientId, fieldNameDuplicateCheck));

            sbScriptBlock.AppendLine(
                string.Format("{0}.ValidateDuplicate = ValidateDuplicate;",
                currentClientId, fieldNameDuplicateCheck));

            sbScriptBlock.AppendLine(
         string.Format("{0}.DuplicateStatusCtrl = document.getElementById('{1}');",
         currentClientId, hdnDuplicateStatus.ClientID));
        }

        sbScriptBlock.AppendLine("</script>");

        scriptBlockLiteral.Text = sbScriptBlock.ToString();

        if (!isSetDisplayMethodCalled)
        {
            SetDisplay();
        }

    }

    private void SetDisplay()
    {
        label.Text = Text;
    }

    
    public string Value
    {
        get
        {
            string val = (isReadOnly && string.IsNullOrEmpty(textBox.Text)) ? hdnCurrent.Value : textBox.Text;

            return (val == string.Empty && isEmptyMeansNull) ?
                 null :
                 val;
        }
        set
        {
            textBox.Text = 
                hdnCurrent.Value = 
                hdnOriginal.Value = string.Empty;
            if (value != null)
            {
                textBox.Text =
                    hdnCurrent.Value =
                    hdnOriginal.Value = value;
            }



            SetDisplay();
            isSetDisplayMethodCalled = true;
        }
    }
    public string OriginalValue
    {
        get
        {
            string val = hdnOriginal.Value;
            return (val == string.Empty && isEmptyMeansNull) ?
                 null :
                 val;
        }
    }
    


    private bool isEmptyMeansNull = true;
    public bool IsEmptyMeansNull
    {
        get { return isEmptyMeansNull; }
        set { isEmptyMeansNull = value; }
    }

    public int MaxLength
    {
        get
        {
            return textBox.MaxLength;
        }
        set
        {
            textBox.MaxLength = value;
        }
    }

    public TextBoxMode TextMode
    {
        get
        {
            return textBox.TextMode;
        }
        set
        {
            textBox.TextMode = value;
        }
    }

    public string Expression
    {
        get
        {
            return vldrRegx.ValidationExpression;
        }
        set
        {
            vldrRegx.ValidationExpression = value;
        }
    }

    public string ErrorMessage
    {
        get
        {
            return vldrRegx.ErrorMessage;
        }
        set
        {
            vldrRegx.ErrorMessage = value;
        }
    }

    

    public string ValidationGroup
    {
        get
        {
            return textBox.ValidationGroup;
        }
        set
        {
            textBox.ValidationGroup =
                vldrRegx.ValidationGroup =
                vldrRequired.ValidationGroup = value;
        }
    }


    #region IASCXControl Members

     public string DisplayCssClass
    {
        get
        {
            return label.CssClass;
        }
        set
        {
            label.CssClass = value;
        }
    }

    private bool isReadOnly = false;
    public bool IsReadOnly
    {
        get
        {
            return isReadOnly;
        }
        set
        {
            isReadOnly = value;
            if (isReadOnly)
            {
                multiView.SetActiveView(viewReadOnly);
            }
            else
            {
                multiView.SetActiveView(viewEditable);
            }
        }
    }

    object IASCXControl.Value
    {
        get
        {
            return Value;
        }
        set
        {
            Value = value as string;
        }
    }
    object IASCXControl.OriginalValue
    {
        get
        {
            return OriginalValue;
        }
        
    }
    public bool HasValueChanged
    {
        get
        {
            return (Value != OriginalValue);
        }
    }

    public string Text
    {
        get
        {
            string val = Value;
            string str = string.Empty;
            if (val != null)
            {
                str = val.ToString();
            }
            return str;
        }
    }

    public Unit Height
    {
        get
        {
            return textBox.Height;
        }
        set
        {
            textBox.Height = value;
        }

    }

    public string OriginalClientId
    {
        get
        {
            return hdnOriginal.ClientID;
        }
    }
    public string CurrentClientId
    {
        get
        {
            return hdnCurrent.ClientID;
        }
    }
    public string InputClientId
    {
        get
        {
            return textBox.ClientID;
        }
    }
    public string InputUniqueId
    {
        get
        {
            return textBox.UniqueID;
        }
    }
    public string DisplayClientId
    {
        get
        {
            return label.ClientID;
        }
    }
    public string DisplayUniqueId
    {
        get
        {
            return label.UniqueID;
        }
    }

    public Control InputControl
    {
        get
        {
            return textBox;
        }
    }

    public string JSResetFunction
    {
        get
        {
            return "ResetTextBox";
        }
    }

    private bool isRequired = false;
    public bool IsRequired
    {
        get
        {
            return isRequired;
        }
        set
        {
            isRequired = value;

            if (isRequired)
            {
                textBox.CssClass = "requiredField";
                vldrRequired.Enabled = true;
                vldrRequired.Display = ValidatorDisplay.Dynamic;
            }
            else
            {
                textBox.CssClass = "";
                vldrRequired.Enabled = false;
                vldrRequired.Display = ValidatorDisplay.None;
            }
        }
    }
    

    private string caption = string.Empty;
    public string Caption
    {
        get
        {
            return caption;
        }
        set
        {
            caption = value;
        }
    }


    private string fieldNameDuplicateCheck = string.Empty;
    public string FieldNameDuplicateCheck
    {
        get
        {
            return fieldNameDuplicateCheck;
        }
        set
        {
            fieldNameDuplicateCheck = value;
        }
    }

    private string entityNameDuplicateCheck = string.Empty;
    public string EntityNameDuplicateCheck
    {
        get
        {
            return entityNameDuplicateCheck;
        }
        set
        {
            entityNameDuplicateCheck = value;
        }
    }


    private bool duplicateCheck = false;
    public bool DuplicateCheck
    {
        get
        {
            return duplicateCheck;
        }
        set
        {
            duplicateCheck = value;
        }
    }

    

    #endregion


    public string InputSkinID
    {
        get
        {
            return textBox.SkinID;
        }
        set
        {
            textBox.SkinID = value;
        }
    }
}
